﻿/*******************************************************************************
 * Copyright © 2016-2020 北京网非科技有限公司 版权所有
 * Author: 北京网非科技有限公司
 * Description: 网非快速开发平台NET
 * Website：http://www.wfdot.com
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CubicFWNet.Application.SystemManage;
using CubicFWNet.Mvc.Filter;
using CubicFWNet.Mvc.Models;
using CubicFWNet.Common;
using CubicFWNet.Data.SystemManage.Model;
using Webdiyer.WebControls.Mvc;

namespace CubicFWNet.Mvc.Areas.SystemManage.Controllers
{
    public class RoleController : ControllerBase
    {
        private RoleApp _app;
        private ModuleApp _moduleApp;
        public RoleController()
        {
            _app = new RoleApp();
            _moduleApp = new ModuleApp();
        }

        //[Authenticate]
        [HttpGet]
        public ActionResult Index(string keyWord = "", int id = 1)
        {
            int pageSize = Application.UserProfile.GetPageSize(this.CurrentUserId);
            var result = _app.GetList(keyWord).ToPagedList<Role>(id, pageSize);
            if (Request.IsAjaxRequest())
            {
                //System.Threading.Thread.Sleep(2000);  //用于测试加载特效
                return PartialView("_AjaxRoleTable", result);
            }
            return View(result);             
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(Guid id)
        {
            var data = _app.GetForm(id);
            return Content(data.ToJson());
        }
        [Delete]
        [HttpPost]
        [HandlerAjaxOnly]
        public string Delete(Guid id)
        {
            return _app.Delete(id).ToJson();
        }

        //添加或修改角色
        [HttpPost]
        [HandlerAjaxOnly]
        [Insert]
        public string Save(Role role, string moduleIds)
        {
            role.CreatorId = this.CurrentUserId;
            Result.Data = role.TODescriptionJson();
            if(!ModelState.IsValid)
            {
                Result.Status = ResultType.error.ToString();
                Result.Message = this.GetFirstModelError();
                return Result.ToJson();
            }
            else
            {                
                _app.AddOrUpdate(role, moduleIds.Split(','));
                return Result.ToJson();
            }
        }

        public ActionResult GetPermissionTree(Guid roleId)
        {
            var moduledata = _moduleApp.GetList();
            var authorizeModule = new List<Guid>();
            if (roleId != Guid.Empty)
            {
                authorizeModule = _app.Load(roleId);
            }
            var treeList = new List<TreeViewModel>();
            foreach (Module item in moduledata)
            {
                TreeViewModel tree = new TreeViewModel();
                bool hasChildren = moduledata.Count(t => t.ParentId == item.Id) == 0 ? false : true;
                tree.id = item.Id.ToString();
                tree.text = item.Name;
                tree.value = item.Id.ToString();
                tree.parentId = item.ParentId.ToString();
                tree.isexpand = true;
                tree.complete = true;
                tree.showcheck = true;
                tree.checkstate = authorizeModule.Count(t => t == item.Id);
                tree.hasChildren = true;
                tree.img = "";
                treeList.Add(tree);
            }
            return Content(treeList.TreeViewJson());
        }
    }
}